/*
 * Copyright 2009 Jiwei Xu (http://www.xujiwei.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

provide("Objects.PolygonObj");

require("Objects.FigureObj");

PolygonObj = Class(FigureObj, (function() {

    var canvas = document.createElement("canvas");
    var context = canvas.getContext("2d");

    return {
        Initialize : function() {
            this.Base();

            this.Handles = [];
        },

        HitTest : function(pt) {
            var firstHandle = this.Handles[0];

            context.beginPath();
            context.moveTo(firstHandle.X, firstHandle.Y);

            for (var i = 1; i < this.Handles.length; ++i) {
                var handle = this.Handles[i];
                context.lineTo(handle.X, handle.Y);
            }

            // close the path
            context.closePath();

            var ret = context.isPointInPath(pt.X, pt.Y);

            return ret;
        },

        Draw : function(ctx) {
            this.SetStyles(ctx);
            
            var firstHandle = this.Handles[0];

            ctx.beginPath();
            ctx.moveTo(firstHandle.X, firstHandle.Y);

            for (var i = 1; i < this.Handles.length; ++i) {
                var handle = this.Handles[i];
                ctx.lineTo(handle.X, handle.Y);
            }

            // close the path
            //ctx.lineTo(firstHandle.X, firstHandle.Y);
            ctx.closePath();

            if (this.IsFill) {
                ctx.fill();
            }
            ctx.stroke();
        }
    };
})());